import 'dart:math';
import 'package:flutter/material.dart';

class SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  SliverAppBarDelegate({
    required this.minHeight,
    required this.maxHeight,
    required this.child,
    required this.overlapsContentVal,
  });

  final double minHeight;
  final double maxHeight;
  final Widget child;
  final ValueNotifier<bool> overlapsContentVal;

  @override
  double get minExtent => minHeight;

  @override
  double get maxExtent => max(maxHeight, minHeight);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    // 你可以根据这个状态来执行其他逻辑，比如设置背景颜色或改变透明度等

    print(overlapsContent);
    if (overlapsContentVal.value != overlapsContent) {
      overlapsContentVal.value = overlapsContent;
    }
    return SizedBox.expand(child: child);
  }

  @override
  bool shouldRebuild(SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
        minHeight != oldDelegate.minHeight ||
        child != oldDelegate.child;
  }
}
